package com.appcenter.cwegportal.service;

import com.appcenter.cwegportal.input.menu.*;
import com.appcenter.cwegportal.mapper.MenusMapper;
import com.appcenter.cwegportal.model.Menus;
import com.appcenter.cwegportal.output.menu.*;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;
@Service
public class MenusService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    MenusMapper MenusMapper;


    @Transactional
    public CreateMenuOutput CreateMenu(CreateMenuInput input){

        logger.error("123123123");

        CreateMenuOutput output = new CreateMenuOutput();


        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        Menus menu = new Menus();
        menu.setId(uuid);
        menu.setTitle(input.getItem().getTitle());
        menu.setDesce(input.getItem().getDesce());

        MenusMapper.insertSelective(menu);
        output.setItem(menu);
        return output;
    }

    @Transactional
    public EditMenuOutput EditMenu(EditMenuInput input){
        EditMenuOutput output=new EditMenuOutput();
        Menus oldMenu= MenusMapper.selectByPrimaryKey(input.getItem().getId());
        oldMenu.setTitle(input.getItem().getTitle());;
        oldMenu.setDesce(input.getItem().getDesce());
        MenusMapper.updateByPrimaryKey(oldMenu);
        output.setItem(oldMenu);
        return  output;
    }


    public DeleteMenuOutput DeleteMenu(DeleteMenuInput input) {
        DeleteMenuOutput output = new DeleteMenuOutput();
        MenusMapper.deleteByPrimaryKey(input.getItem().getId());
        return output;
    }

    public GetMenusOutput GetMenus(GetMenusInput input) {
        GetMenusOutput output = new GetMenusOutput();

        logger.error("log4j2 error!");

        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"Id desc");
        //List<Menus> ls = MenuMapper.SearchMenus(input);
        List<Menus> ls=MenusMapper.selectAll();
        PageInfo<Menus> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<Menus>(page.getList(),(int)page.getTotal()));
        return  output;

    }

    public PageLoadOutput PageLoad(PageLoadInput input) {
        PageLoadOutput output = new PageLoadOutput();
        output.setItems(GetMenus(input).getItems());
        return output;
    }
}
